<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Db;
use think\Model as ThinkModel;

//多种排位模型
class ManyQueueRule extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__QT_MANY_QUEUE_RULE__';
    protected $name = 'qt_many_queue_rule';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;


    /**
     * 获取多种排位
     * @param $storeId
     * @return ManyQueueRule[]
     *
     */
    public function getList($storeId){
        $data = $this
            ->field('mqr.*,u.real_name')
            ->alias('mqr')
            ->leftJoin('qt_users u','mqr.user_id = u.id')
            ->where('mqr.store_id',$storeId)
            ->order('mqr.id asc')
            ->select();
        foreach ($data as $key=>$value){
            //处理项目
            $project_ids = explode(',',$value['project_ids']);
            $projectName = Db::name('qt_project')->whereIn('id',$project_ids)->column('name');
            $data[$key]['project_name'] = implode(',',$projectName);
            //处理工种
            $work_type_ids = explode(',',$value['work_type_ids']);
            $workTypeName = Db::name('qt_work_type')->whereIn('id',$work_type_ids)->column('title');
            $data[$key]['work_type_name'] = implode(',',$workTypeName);
        }
        return $data;
    }


    /**
     * 添加多种排位
     * @param $storeId
     * @param $data
     */
    public function saveData($storeId,$data){
        if(isset($data['id']) && !empty($data['id'])){
            unset($data['create_time']);
            $data['update_time'] = time();
            Db::name('qt_many_queue_rule')->where('id',$data['id'])->update($data);
            $manyQueueId = $data['id'];
        }else{
            $data['store_id'] = $storeId;
            $data['create_time'] = time();
            $manyQueueId = Db::name('qt_many_queue_rule')->insertGetId($data);
        }
        $projects = explode(',',$data['project_ids']);
        foreach ($projects as $key=>$value){
            $workType = explode(',',$data['work_type_ids']);
            $userIds = Db::name('qt_users')
                ->field('u.id,q.sort')
                ->alias('u')
                ->leftJoin('qt_order_queue q','u.id = q.user_id')
                ->where(['u.store_id'=>$storeId])
                ->whereIn('u.top_type_id',$workType)
                ->order('q.sort asc')
                ->select();
            $insertData = [];
            $i = 1;
            $j= 0;
            foreach ($userIds as $k=>$v){
                $insertData[$j]['store_id'] = $storeId;
                $insertData[$j]['user_id'] = $v['id'];
                $insertData[$j]['many_queue_id'] = $manyQueueId;
                $insertData[$j]['many_project_id'] = $value;
                $insertData[$j]['sort'] = $i;
                $insertData[$j]['created_time'] = time();
                $i ++;
                $j ++;
            }
            Db::name('qt_order_many_queue')->insertAll($insertData);
        }
        return true;


    }


}